/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */

import { Project } from '@ohos/hvigor';
import path from 'path';

import { BundleType, CommonConst } from '../../const/common-const.js';
import { ModuleModelImpl } from '../module/module-model-impl.js';
import { AppResModelImpl } from '../res/app-res-model-impl.js';
import { AppRes } from '../res/res-model.js';

import { CoreProjectModelImpl } from './core-project-model-impl.js';
import { StageProjectAppRes } from './project-model.js';

/**
 * Stage模型的工程持久化数据模型,包含工程源码数据,配置数据等
 *
 * @since 2022/1/20
 */
export class ProjectModelImpl extends CoreProjectModelImpl implements StageProjectAppRes {
  private readonly appRes: AppRes;

  constructor(project: Project) {
    super(project);
    this.appRes = new AppResModelImpl(path.resolve(this.projectPath, CommonConst.APP_SCOPE));
  }

  initSubProject(): void {
    const subModules = this.project.getSubModules();
    subModules.forEach((module, name) => {
      this.subModels.set(name, new ModuleModelImpl(module, this));
    });
  }

  /**
   * 获取该工程默认的bundleName值,即AppScope中配置的值
   */
  getDefaultBundleName(): string {
    return this.appRes.getAppResOpt().app ? this.appRes.getAppResOpt().app.bundleName : '';
  }

  getBundleType(): string {
    return this.appRes.getAppResOpt().app ? this.appRes.getAppResOpt().app.bundleType ?? BundleType.APP : BundleType.APP;
  }

  getAppRes(): AppRes {
    return this.appRes;
  }
}
